/*
  S.M.A.C.K - An operating system kernel
  Copyright (C) 2010,2011 Mattias Holm and Kristian Rietveld
  For licensing and a full list of authors of the kernel, see the files
  COPYING and AUTHORS.
*/

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <syscalls.h>

static const char *startup_processes[] =
{
  "/video-ps",
  "/sh",
  /* Must terminate this list with NULL! */
  NULL
};

static int
start(const char *filename)
{
  int pid;
  int ret;

  /* Try if the file exists */
  ret = stat(filename, NULL, NULL);
  if (ret < 0)
    return -1;

  /* Fork off and launch the process */
  pid = fork();
  if (pid == 0)
    execve(filename, "");

  return pid;
}

void stdio_init(void);

static void
mount_devfs()
{
  if (stat ("/dev", NULL, NULL) < 0)
    return;

  mount("devfs", "/dev");

  /* Try this again now devfs is mounted */
  stdio_init();
}

int
main(int argc, char **argv)
{
  int i = 0;

  mount_devfs();

  printf("init launching...\n");

  printf("  spawning startup processes: ");
  while (startup_processes[i] != NULL)
    {
      int pid;

      printf("%s", startup_processes[i]);

      pid = start(startup_processes[i]);

      if (pid >= 0)
        printf("(%d) ", pid);
      else
        printf("(FAILED) ");

      i++;
    }
  printf("\n");

  while (1)
    {
      int pid = wait(NULL);
      /* FIXME: Which child did exit, do we need to restart it? */
    }
}
